﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using HotelManagement.Class;

namespace HotelManagement
{
    public partial class UpdateHousekeepingSchedule : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                getGeneralMaintenanceStaff();
                getEstateMaintenanceStaff();
                getRoomMaintenanceStaff();
                getSecurityMaintenanceStaff();
                displayStaffInformation();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            updateHousekeepingSchedule();
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            Server.Transfer("ManageHousekeepingAndStaff.aspx");
        }
        private void displayStaffInformation()
        {
            //Get query string 
            string qs = Request.QueryString["scheduleID"];
            //string qs = Request.QueryString["StaffID"];
         
          
            //Call getstaffbyid method
            HousekeepingSchedule s = HousekeepingDBManager.getHousekeepingSchedulebyScheduleId(qs);


            //Display the Information 
            lblScheduleID.Text = s.ScheduleID;
            tbxDescription1.Text = s.Description;
            ddlGmS1.SelectedValue = s.GeneralMaintenanceStaffID;
            ddlRmS1.SelectedValue = s.RoomMaintenanceStaffID;
            ddlEmS1.SelectedValue = s.EstateMaintenanceStaffID;
            ddlSmS1.SelectedValue = s.SecurityMaintenanceStaffID;
                   
            DateTime GetDay = s.Date;
            string Day = Convert.ToString(GetDay.Day);
            tbxDay.Text = Day;

            DateTime GetYear = s.Date;
            string Year = Convert.ToString(GetYear.Year);
            tbxYear.Text = Year;

            DateTime GetMonth = s.Date;
            string Month = Convert.ToString(GetMonth.Month);
            ddlMonth.SelectedValue = Month;
        }

        private void getSecurityMaintenanceStaff()
        {
            ddlSmS1.Items.Clear();

            string selectSQL = "SELECT FirstName,LastName, StaffID FROM Staff WHERE DutyType='Security Maintenance'";
            SqlDataReader reader;

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();
                SqlCommand cmd = new SqlCommand(selectSQL, con);
                ListItem newItem = new ListItem();


                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    ddlSmS1.Items.Add(newItem);

                }
                reader.Close();

            }
            catch (SqlException ex1)
            {
                lblStatus.Text = "Error: " + ex1 + "Please try again";
            }
        }

        private void getRoomMaintenanceStaff()
        {
            ddlRmS1.Items.Clear();
            string selectSQL = "SELECT FirstName,LastName, StaffID FROM Staff WHERE DutyType='Room Maintenance'";
            SqlDataReader reader;

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();
                SqlCommand cmd = new SqlCommand(selectSQL, con);
                ListItem newItem = new ListItem();


                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    ddlRmS1.Items.Add(newItem);
                }
                reader.Close();

            }
            catch (SqlException ex1)
            {
                lblStatus.Text = "Error: " + ex1 + "Please try again";
            }
        }

        private void getEstateMaintenanceStaff()
        {
            ddlEmS1.Items.Clear();
            string selectSQL = "SELECT FirstName,LastName, StaffID FROM Staff WHERE DutyType='Estate Maintenance'";
            SqlDataReader reader;

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();
                SqlCommand cmd = new SqlCommand(selectSQL, con);
                ListItem newItem = new ListItem();


                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    ddlEmS1.Items.Add(newItem);

                }
                reader.Close();

            }
            catch (SqlException ex1)
            {
                lblStatus.Text = "Error: " + ex1 + "Please try again";
            }
        }

        private void getGeneralMaintenanceStaff()
        {
            ddlGmS1.Items.Clear();
            string selectSQL = "SELECT FirstName,LastName, StaffID FROM Staff WHERE DutyType='General Maintenance'";

            SqlDataReader reader;
            ListItem newItem = new ListItem();

            try
            {
                SqlConnection con = StaffDBManager.ConnectToDatabase();

                SqlCommand cmd = new SqlCommand(selectSQL, con);

                con.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    newItem = new ListItem();
                    newItem.Text = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    newItem.Value = reader["StaffID"].ToString() + " - " + reader["FirstName"].ToString() + " " + reader["LastName"].ToString();
                    ddlGmS1.Items.Add(newItem);
                }
                reader.Close();

            }
            catch (SqlException ex1)
            {
                lblStatus.Text = "Error: " + ex1 + "Please try again";
            }
        }

        private void updateHousekeepingSchedule()
        {
            try
            {
       
                //Call Update method
                HousekeepingSchedule hs = new HousekeepingSchedule();

                hs.Date = Convert.ToDateTime(tbxYear.Text + "/" + ddlMonth.SelectedValue + "/" + tbxDay.Text);
                hs.ScheduleID = lblScheduleID.Text;
                hs.GeneralMaintenanceStaffID = ddlGmS1.SelectedValue;
                hs.RoomMaintenanceStaffID = ddlRmS1.SelectedValue;
                hs.EstateMaintenanceStaffID = ddlEmS1.SelectedValue;
                hs.SecurityMaintenanceStaffID = ddlSmS1.SelectedValue;
                hs.Description = tbxDescription1.Text;


                //Call the Update Staff method and pass the object  to the method
                //Display message of success or failure to user
                if (HousekeepingDBManager.updateHousekeepingSchedule(hs) == 1)
                {
                    lblStatus.Text = "Details update successfully";
                }
                else
                {
                    lblStatus.Text = "Update failed. Please try again";
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = "The Schedule ID entered is already in the database. Please try again";

            }
        }




        
    }
}